package com.alf.admin.service.impl;

import com.alf.admin.mapper.AdChannelMapper;
import com.alf.admin.service.AdChannelService;
import com.alf.model.admin.dtos.ChannelDto;
import com.alf.model.admin.pojos.AdChannel;
import com.alf.model.common.dtos.PageResponseResult;
import com.alf.model.common.dtos.ResponseResult;
import com.alf.model.common.enums.AppHttpCodeEnum;
import com.alf.utils.common.AssertUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Alf_Chou
 * version1.0
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    /**
     * 根据名字查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //检验
        if(AssertUtil.isNotEmpty(dto)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        dto.checkParam();

        //查询并分页排序
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.<AdChannel>lambdaQuery();

        if(AssertUtil.isNotEmpty(dto.getName())){
            queryWrapper.like(AdChannel::getName,dto.getName());
        }
        if(AssertUtil.isNotEmpty(dto.getStatus())){
            queryWrapper.eq(AdChannel::getStatus,dto.getStatus());
        }
        //升序
        queryWrapper.orderByAsc(AdChannel::getOrd);

        //分页
        Page<AdChannel> pageReq = new Page<>(dto.getPage(),dto.getSize());
        //这里的this用于指明对象
        IPage<AdChannel> pageResult = this.page(pageReq, queryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(),dto.getSize(),pageResult.getTotal());
        return pageResponseResult;

    }

    /**
     * 新增
     * @param channel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel channel) {
        if(channel == null ||AssertUtil.isNotEmpty(channel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //判断频道是否存在
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName,channel.getName()));
        if(count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
        }
        channel.setCreatedTime(new Date());
        this.save(channel);

        return ResponseResult.okResult(channel);
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {

        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdChannel channel = getById(adChannel.getId());
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道信息不存在");
        }
        //校验名称唯一性
        if(AssertUtil.isNotEmpty(adChannel.getName())
                &&
                !adChannel.getName().equals(channel.getName())){
            int count = this.count(Wrappers.<AdChannel>lambdaQuery()
                    .eq(AdChannel::getName, adChannel.getName()));
            if(count > 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
            }
        }
        this.updateById(adChannel);
        //4 返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Integer id) {

        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断当前频道是否存在 和 是否有效
        AdChannel adChannel = getById(id);
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
